<!-- eslint-disable vue/multi-word-component-names -->
<script setup>
import { ref,onMounted } from 'vue';
import axios from 'axios';
import { useUserStore } from '@/stores/user';

// 获取 Pinia Store 实例
const userStore = useUserStore();


// 定义响应式变量
const transactions = ref([]); // 存储交易记录
const currentPage = ref(1); // 当前页码
const totalPages = ref(1); // 总页数
const selectedTransaction = ref(null); // 选中的交易记录，用于弹窗显示

// 请求后端获取交易记录
const fetchTransactions = async () => {
  try {
    const response = await axios.getget(`http://localhost:8080/trades/${userStore.userAInfo.userId}`);
    const data = response.data;
    totalPages.value = Math.ceil(data.length / 10); // 每页10条记录
    transactions.value = data.slice((currentPage.value - 1) * 10, currentPage.value * 10);
  } catch (error) {
    console.error("获取交易记录失败", error);
  }
};

// 日期格式化
const formatDate = (date) => {
  const d = new Date(date);
  return `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()} ${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}`;
};

// 跳转到指定页面
const goToPage = (page) => {
  if (page < 1 || page > totalPages.value) return;
  currentPage.value = page;
  fetchTransactions();
};

// 显示交易详情
const showDetails = (tradeId) => {
  const transaction = transactions.value.find((item) => item.tradeId === tradeId);
  if (transaction) {
    selectedTransaction.value = transaction; // 设置选中的交易记录
  }
};

// 关闭弹窗
const closeModal = () => {
  selectedTransaction.value = null; // 清空选中的交易记录，关闭弹窗
};

// 在组件挂载时获取交易记录
onMounted(() => {
  fetchTransactions();
});
</script>




















<template>
  <div class="buyer-transaction">
    <h2>已买到的宝贝</h2>

    <!-- Transaction List -->
    <div v-if="transactions.length > 0">
      <div class="transaction-item" v-for="transaction in transactions" :key="transaction.tradeId">
        <div class="transaction-info">
          <!-- 商品图片 -->
          <img :src="transaction.goods.imageUrl" alt="商品图片" class="product-image"/>

          <!-- 商品信息 -->
          <div class="product-details">
            <p class="product-name">{{ transaction.goods.goodsName }}</p>
            <p class="order-id">交易时间: {{ formatDate(transaction.tradeTime) }}</p>
            <p class="transaction-time">买家: {{ transaction.buyer?.buyerName }}</p>
          </div>

          <!-- 价格、数量、状态 -->
          <div class="transaction-status">
            <p>数量: {{ transaction.tradeQuantity }}</p>
            <p class="total-price">总价: ¥{{ (transaction.tradeQuantity * transaction.goods.price).toFixed(2) }}</p>
<!--            <span :class="['status', transaction.status]">{{ transaction.status || '已完成' }}</span>-->
          </div>
        </div>

        <!-- 交易详情按钮 -->
        <button @click="showDetails(transaction.tradeId)" class="detail-btn">详情</button>
      </div>

      <!-- 分页 -->
      <div v-if="totalPages > 1" class="pagination">
        <button @click="goToPage(currentPage - 1)" :disabled="currentPage === 1">上一页</button>
        <span>{{ currentPage }} / {{ totalPages }}</span>
        <button @click="goToPage(currentPage + 1)" :disabled="currentPage === totalPages">下一页</button>
      </div>
    </div>

    <div v-else class="no-transactions">
      <p>您还没有购买任何商品。</p>
    </div>

    <!-- 弹窗展示详情 -->
    <div v-if="selectedTransaction" class="modal-overlay" @click.self="closeModal">
      <div class="modal">
        <h3>交易详情</h3>
        <div class="modal-content">
          <p><strong>买家电话:</strong> {{ selectedTransaction.buyer.buyerPhone }}</p>
          <p><strong>买家地址:</strong> {{ selectedTransaction.buyer.tradeAddress }}</p>
          <p><strong>商品描述:</strong> {{ selectedTransaction.goods.description }}</p>
          <p><strong>卖家:</strong> {{ selectedTransaction.goods.seller.storeName }} | {{ selectedTransaction.goods.seller.phoneNumber }}</p>
          <p><strong>商品类别:</strong> {{ selectedTransaction.goods.priCategory }} > {{ selectedTransaction.goods.secCategory }}</p>
        </div>
        <button @click="closeModal" class="close-btn">关闭</button>
      </div>
    </div>
  </div>
</template>



<style scoped>
.buyer-transaction {
  padding: 20px;
  background-color: #f8f9fa;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

h2 {
  font-size: 26px;
  font-weight: bold;
  color: #333;
  margin-bottom: 30px;
  text-align: center;
  text-transform: uppercase;
}

.transaction-item {
  background-color: #fff;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
}

.transaction-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 6px 14px rgba(0, 0, 0, 0.1);
}

.transaction-info {
  display: flex;
  align-items: center;
}

.product-image {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 8px;
  margin-right: 20px;
}

.product-details {
  flex-grow: 1;
  overflow: hidden;
}

.product-name {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin-bottom: 5px;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}

.order-id,
.transaction-time {
  font-size: 14px;
  color: #888;
}

.transaction-status {
  text-align: left;
  margin-left: 100px;
}

.transaction-status p {
  font-size: 16px;
  color: #555;
  margin: 5px 0;
}

.total-price {
  font-size: 18px;
  font-weight: 700;
  color: #e74c3c;
}

.detail-btn {
  padding: 6px 12px;
  background-color: #3498db;
  color: white;
  border: none;
  border-radius: 20px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
  position: absolute;
  width: 60px;
  bottom: 20px;
  right: 20px;
}

.detail-btn:hover {
  background-color: #2980b9;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal {
  background-color: #fff;
  padding: 20px;
  border-radius: 10px;
  width: 60%; /* 弹窗宽度 */
  max-width: 600px;
  max-height: 80%;
  overflow-y: auto;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.modal-content p {
  font-size: 16px;
  margin-bottom: 10px;
}

.close-btn {
  padding: 8px 16px;
  background-color: #e74c3c;
  color: white;
  border: none;
  border-radius: 25px;
  cursor: pointer;
  font-size: 14px;
  width: 100%;
  transition: all 0.3s ease;
  margin-top: 20px;
}

.close-btn
{
  background-color: #c0392b;
}

.pagination
{ display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 30px;
}

.pagination button {
  padding: 8px 16px;
  background-color: #3498db;
  color: white;
  border: none;
  border-radius: 25px;
  cursor: pointer;
  font-size: 14px;
  margin: 0 10px;
  transition: all 0.3s ease;
}

.pagination button
{
  background-color: #d3d3d3;
  cursor: not-allowed;
}

.pagination button
{
  background-color: #2980b9;
}

.no-transactions {
  text-align: center;
  font-size: 18px;
  color: #888;
  margin-top: 50px;
  padding: 40px;
  background-color: #fff; border-radius: 10px;
  box-shadow: 0 6px 14px rgba(0, 0, 0, 0.1);
}
</style>